Enviar correos en C# con MailKit

83

Enviar correos en C# con MailKit -

[HttpPost]
public ActionResult<IEnumerable<bool>> 
SendEmail([FromBody] string emailData)
{
    try
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("TheCodeBuzz", "[email protected]"));
        message.To.Add(new MailboxAddress("TheCodeBuzz", "infoATthecodebuzz.com"));
        message.Subject = "My First Email";
        message.Body = new TextPart("plain")
        {
            Text = emailData
        };
        
            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
 
                client.Connect("smtp.gmail.com", 587, false);
 
                //SMTP server authentication if needed
                client.Authenticate("[email protected]", "xxxxx");
 
                client.Send(message);
 
                client.Disconnect(true);
            }
 
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return StatusCode(500, "Error occured");
    }
 
    return Ok(true);
}

Comments

Submit
0 Comments